﻿using System.Windows.Media;

namespace YY.Admin.Core.Helper
{
    public static class UIHelper
    {
        /// <summary>
        /// 等待 WPF 完成下一次渲染帧，确保 UI 更新已经完成
        /// </summary>
        /// <returns></returns>
        public static async Task WaitForRenderAsync()
        {
            var tcs = new TaskCompletionSource<object?>();

            EventHandler handler = null!;
            handler = (s, e) =>
            {
                // 取消订阅
                CompositionTarget.Rendering -= handler;
                // 标记任务完成
                tcs.TrySetResult(null);
            };

            // 订阅渲染事件
            CompositionTarget.Rendering += handler;

            // 等待下一次渲染
            await tcs.Task;
        }
    }
}
